/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.spring.initilizer;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.springframework.core.io.Resource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextListener;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.web.io.HttpServletRequestProxyFactory;
import com.genia.toolbox.web.io.HttpSessionProxy;
import com.genia.toolbox.web.servlet.CustomContextLoaderListener;

public class ProcessWebInitializer
{

  /**
   * 
   */
  private static final RequestContextListener REQUEST_CONTEXT_LISTENER = new RequestContextListener();

  /**
   * 
   */

  public static final String CONTEXT_PATH = "/contextPath";

  private static final ServletContext servletContext = new DumbServletContext();

  private static final transient Mockery context = new Mockery();

  private static final transient HttpServletRequest baseRequest = context.mock(HttpServletRequest.class);

  private static final transient HttpSession baseSession = context.mock(HttpSession.class);

  static {
    context.checking(new Expectations()
    {
      {
        final HttpSession httpSession = new HttpSessionProxy(baseSession);
        allowing(baseRequest).getContextPath();
        will(returnValue(CONTEXT_PATH));
        allowing(baseRequest).getSession();
        will(returnValue(httpSession));
        allowing(baseRequest).getSession(with(any(boolean.class)));
        will(returnValue(httpSession));
        allowing(baseRequest).getLocale();
        will(returnValue(Locale.getDefault()));
        allowing(baseRequest).getAttribute(with(any(String.class)));
        will(returnValue(null));
        allowing(baseSession).getAttribute(with(any(String.class)));
        will(returnValue(null));
      }
    });
  }



  private static HttpServletRequest getRequest()
  {
    return HttpServletRequestProxyFactory.getHttpServletRequestProxy(baseRequest);
  }



  /**
   * generate an <code>WebApplicationContext</code> from a config file.
   * 
   * @param resource
   *          the location of the config file
   * @return the <code>ApplicationContext</code>
   */
  public static WebApplicationContext getWebApplicationContext(final Resource resource)
  {
    WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (webApplicationContext == null) {
      new CustomContextLoaderListener().contextInitialized(new ServletContextEvent(servletContext));
      webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    }
    /*
     * GenericWebApplicationContext webApplicationContext = new
     * GenericWebApplicationContext();
     * webApplicationContext.setServletContext(servletContext);
     * webApplicationContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
     * webApplicationContext); XmlBeanDefinitionReader xmlReader = new
     * XmlBeanDefinitionReader(webApplicationContext);
     * xmlReader.loadBeanDefinitions(resource); webApplicationContext.refresh();
     */
    return webApplicationContext;
  }



  public static void initInitialiseRequest(final WebApplicationContext webApplicationContext)
  {
    initInitialiseRequest(webApplicationContext, getRequest());
  }



  public static void initInitialiseRequest(final WebApplicationContext webApplicationContext, HttpServletRequest request)
  {
    REQUEST_CONTEXT_LISTENER.requestInitialized(new ServletRequestEvent(webApplicationContext.getServletContext(), request));
  }



  /**
   * getter for the webApplicationContext property.
   * 
   * @return the webApplicationContext
   * @throws BundledException
   *           when an unexpected error occurred
   */
  public static WebApplicationContext getWebApplicationContext()
  {
    Object attribute = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    WebApplicationContext webApplicationContext = (WebApplicationContext) attribute;
    if (webApplicationContext == null) {
      new CustomContextLoaderListener().contextInitialized(new ServletContextEvent(servletContext));
      attribute = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
      if(attribute instanceof Exception) {
        ((Exception) attribute).printStackTrace();
      }
      webApplicationContext = (WebApplicationContext) attribute;
    }
    return webApplicationContext;
  }

}
